package com.itcast.excel.controller;

import com.itcast.excel.entity.CourseEntity;
import com.itcast.excel.service.ICourseService;
import lombok.extern.slf4j.Slf4j;
import org.basic.common.entity.SheetEntity;
import org.basic.common.utils.CommonExportUtils;
import org.basic.common.utils.CommonImportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 测试Controller
 *
 * @author zheng.zhang
 */
@Slf4j
@RestController
@RequestMapping("excel")
public class DemoController {
    @Autowired
    private ICourseService courseService;

    /**
     * 测试接口
     *
     * @param response 响应结果
     */
    @GetMapping("simple-excel")
    public void simpleExcel(HttpServletResponse response) {
        try {
            List<CourseEntity> entityList = Optional.ofNullable(courseService.query()).orElse(new ArrayList<>());
            CommonExportUtils.exportExcel(SheetEntity.builder()
                    .data(entityList)
                    .title("测试导出")
                    .fileName(System.currentTimeMillis() + "")
                    .isCreateHeader(true)
                    .pojoClass(CourseEntity.class)
                    .sheetName("第一页")
                    .build(), response);
        } catch (IOException e) {
            log.error("导出失败:{}", e.getMessage());
        }
    }

    /**
     * 多sheet页导出
     *
     * @param response 响应结果
     */
    @GetMapping("multiple-excel")
    public void multipleExcel(HttpServletResponse response) {
        List<CourseEntity> firstData = Optional.ofNullable(courseService.query()).orElse(new ArrayList<>());
        Map<String, Object> firstPage = CommonExportUtils.createOneSheet("第一页", "第一页签", CourseEntity.class, firstData);

        List<CourseEntity> secondData = Optional.ofNullable(courseService.query()).orElse(new ArrayList<>());
        Map<String, Object> secondPage = CommonExportUtils.createOneSheet("第二页", "第二页签", CourseEntity.class, secondData);

        try {
            CommonExportUtils.exportExcel(Arrays.asList(firstPage, secondPage), System.currentTimeMillis() + "", response);
        } catch (IOException e) {
            log.error("导出失败:{}", e.getMessage());
        }
    }

    /**
     * 导入Excel
     *
     * @return 导入结果
     */
    @GetMapping("import-excel")
    public ResponseEntity<List<CourseEntity>> importExcel() {
        List<CourseEntity> entityList = new ArrayList<>();
        try {
            entityList = CommonImportUtils.importExcel("C:\\Users\\zheng.zhang\\Downloads\\1605599630590.XLSX", 1, 1, CourseEntity.class);
        } catch (IOException e) {
            log.error("导入失败:{}", e.getMessage());
        }
        return ResponseEntity.ok(entityList);
    }

    /**
     * 模板导出Excel
     */
    @GetMapping("template-excel")
    public void templateExport(HttpServletResponse response) {
        List<CourseEntity> dataList = Optional.ofNullable(courseService.query()).orElse(new ArrayList<>());
        Map<String, Object> params = new HashMap<>(8);
        params.put("list", dataList);
        params.put("name", "张三");
        try {
            //String url = "http://39.97.231.147:9000/aliyun-file/tepmlate.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20201118%2F%2Fs3%2Faws4_request&X-Amz-Date=20201118T023908Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=4c5e021293dfb5bbbc7907509535f05610a8680529d44f903a53ba00f2389798";
            String url = "C:\\Users\\zheng.zhang\\Desktop\\File\\excel\\template.xlsx";
            CommonExportUtils.exportExcel(url, params, System.currentTimeMillis() + "", response);
        } catch (IOException e) {
            log.error("导出失败:{}", e.getMessage());
        }
    }
}
